$(function () {
    $("#jqGrid").jqGrid({
        url: '../medicalenjoymanagement/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '姓名', name: 'name', index: 'name', width: 80 }, 			
			{ label: '身份证号', name: 'idno', index: 'idno', width: 80 }, 			
			{ label: '年龄', name: 'age', index: 'age', width: 80 }, 			
			{ label: '性别', name: 'sex', index: 'sex', width: 80 ,
                formatter:function(v){
                    if(v=="0"){
                        return "男";
                    }else if(v=="1"){
                        return "女";
                    }else{
                        return ""
                    }
                } },
			{ label: '联系电话', name: 'telephone', index: 'Telephone', width: 80 }, 			
			{ label: '用工单位编号', name: 'companyid', index: 'companyid', width: 80 ,hidden:true},
			{ label: '用工单位名称', name: 'company', index: 'company', width: 80 },
			{ label: '报备类型', name: 'preparationType', index: 'Preparation_type', width: 80 ,formatter:getDictInfo},
			{ label: '报备医院', name: 'preparationHospital', index: 'Preparation_hospital', width: 80 }, 			
			{ label: '报备日期', name: 'preparationDate', index: 'Preparation_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}
            },
			{ label: '票价提交日期', name: 'fareSubmissionDate', index: 'Fare_submission_date',  width: 122, formatter:"date", formatoptions: {newformat:'Y-m-d'}
            },
			{ label: '发票金额', name: 'invoiceAmount', index: 'invoice_amount', width: 80 }, 			
			{ label: '报备回单日期', name: 'receiptDate', index: 'receipt_date',  width: 120, formatter:"date", formatoptions: {newformat:'Y-m-d'}
            },
			{ label: '送单日期', name: 'deliveryDate', index: 'Delivery_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}
            },
			{ label: '回单金额', name: 'receiptAmount', index: 'receipt_amount', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getDictList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            company: null,
            name: null
        },
        showList: true,
        showtable: false,
        showtable1: false,
        showtable2: false,
        showtable3: false,
        showtable4: false,
		title: null,
        dictList:{},
		medicalEnjoyManagement: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable = false;
            vm.title = "新增";
			vm.title = "新增";
			vm.medicalEnjoyManagement = {
                name:""
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.showtable = false;
            vm.title = "新增";
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            vm.medicalEnjoyManagement.preparationDate=$("#preparationDate").val();
            vm.medicalEnjoyManagement.fareSubmissionDate=$("#fareSubmissionDate").val();
            vm.medicalEnjoyManagement.receiptDate=$("#receiptDate").val();
            vm.medicalEnjoyManagement.deliveryDate=$("#deliveryDate").val();
            var url = vm.medicalEnjoyManagement.id == null ? "../medicalenjoymanagement/save" : "../medicalenjoymanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.medicalEnjoyManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../medicalenjoymanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../medicalenjoymanagement/info/"+id, function(r){
                vm.medicalEnjoyManagement = r.medicalEnjoyManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'name': vm.q.name,'preparationHospital':vm.q.preparationHospital,
                    'company': vm.q.company,'telephone':vm.q.telephone},
                page:page
            }).trigger("reloadGrid");
		},
        bbqr: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = true;
            vm.showtable2 = false;
            vm.showtable3 = false;
            vm.showtable4 = false;
            vm.title = "报备确认";

            vm.getInfo(id)
        },
        pjqr: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = false;
            vm.showtable2 = true;
            vm.showtable3 = false;
            vm.showtable4 = false;
            vm.title = "票据确认";

            vm.getInfo(id)
        },
        hdqr: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = false;
            vm.showtable2 = false;
            vm.showtable3 = true;
            vm.showtable4 = false;
            vm.title = "回单确认";

            vm.getInfo(id)
        },
        sdqr: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = false;
            vm.showtable2 = false;
            vm.showtable3 = false;
            vm.showtable4 = true;
            vm.title = "送达确认";

            vm.getInfo(id)
        },
        popPage: function(){
            // utils.popGrid();
            popPage.popAllUser();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['800px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.medicalEnjoyManagement.name=rowData.name;
                    vm.medicalEnjoyManagement.idno=rowData.idno;
                    vm.medicalEnjoyManagement.company=rowData.company;
                    vm.medicalEnjoyManagement.companyid=rowData.companyId;
                    // vm.reproductiveEnjoyManagement.dept=rowData.department;
                    vm.medicalEnjoyManagement.age=rowData.age;
                    var sex=rowData.sex;
                    if(sex=='男'){
                        sex="0";
                    }else if(sex=='女'){
                        sex="1";
                    }
                    vm.medicalEnjoyManagement.sex=sex;
                    vm.medicalEnjoyManagement.telephone=rowData.telphone;

                    //alert(JSON.stringify( vm.safetyEducationManagement));


                    layer.close(index);
                }
            });
        },
        getDictList: function(){
            var params = {
                dicttype:"bblx1"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList = r.bblx1;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
	}
});